package com.yunshipei.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.yunshipei.enterplorer.R;
import com.yunshipei.db.DatabaseUtil;
import com.yunshipei.model.NotifyInfo;
import com.yunshipei.utils.AlarmSet;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import io.rong.app.utils.StringUtilSub;

/**
 * Created by cike on 15-11-20.
 */
public class NotificationAdapter extends BaseAdapter {
    private Context context;
    private List<NotifyInfo> list = null;
    private ListView mListView;
    private HashMap<Integer, Boolean> isSelected;

    public NotificationAdapter(Context context, List<NotifyInfo> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (list != null) {
            return this.list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void updateListView(List<NotifyInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (mListView == null) {
            mListView = (ListView) parent;
        }
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.notifi_item, null);
            viewHolder.contentView = (TextView) convertView.findViewById(R.id.content_view);
            //viewHolder.timeView = (TextView) convertView.findViewById(R.id.time_view);
            viewHolder.switchView = (CheckBox) convertView.findViewById(R.id.switch_View);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (!this.list.get(position).getSpeechRecognition().getDateTime().getTimeOrig().equals(null)) {
            viewHolder.contentView.setText(StringUtilSub.getInstance()
                    .subSting(0, this.list.get(position).getSpeechRecognition().getDateTime().getTimeOrig() + "," + this.list.get(position).getSpeechRecognition().getContent(), 15));
        } else {
            viewHolder.contentView.setText(StringUtilSub.getInstance().subSting(0, this.list.get(position).getSpeechRecognition().getContent(), 10));
        }
        if (!TextUtils.isEmpty(list.get(position).getNotifiStatus())) {
            viewHolder.switchView.setChecked(Boolean.valueOf(list.get(position).getNotifiStatus()).booleanValue());
        }
        viewHolder.switchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkBox = (CheckBox) v;
                DatabaseUtil dbu = new DatabaseUtil();
                dbu.updateNotifiStatus(list.get(position).get_id(), String.valueOf(checkBox.isChecked()));
                if (checkBox.isChecked()) {
                    list.get(position).setAlarm(context);
                } else {
                    AlarmSet as = new AlarmSet(context);
                    //关闭提醒
                    as.cancelAlarm(list.get(position));
                }
                if (list.get(position).getSpeechRecognition().getDateTime().getInfactDate().before(new Date()) &&
                        list.get(position).getRepetitionTime().length() == 0) {
                    list.remove(position);
                    notifyDatasetChanged();
                }
            }
        });

        return convertView;
    }

    public void notifyDatasetChanged() {
        this.notifyDataSetChanged();
    }

    class ViewHolder {
        TextView contentView;
        //TextView timeView;
        CheckBox switchView;

        public TextView getContentView() {
            return contentView;
        }

        public void setContentView(TextView contentView) {
            this.contentView = contentView;
        }

        public CheckBox getSwitchView() {
            return switchView;
        }

        public void setSwitchView(CheckBox switchView) {
            this.switchView = switchView;
        }
    }

    public NotificationAdapter getAdapter() {
        return this;
    }

    public HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        this.isSelected = isSelected;
    }

}
